<?php

namespace app\admin\controller\mall;

use app\admin\model\MallBanner;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use EasyAdmin\tool\CommonTool;
use jianyan\excel\Excel;
use think\App;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="首页轮播图")
 */
class Banner extends AdminController
{

//    use \app\admin\traits\Curd;
    
    protected $relationSearch = true;
    public function __construct(App $app)
    {
        parent::__construct($app);
        
        $this->model = new MallBanner();
        
        $this->assign('getStatusList', $this->model->getStatusList());
        
    }
    
    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            [$page, $limit, $where] = $this->buildTableParames();
            $count = $this->model
                ->withJoin('goods', 'LEFT')
                ->where($where)
                ->count();
            $list  = $this->model
                ->withJoin('goods', 'LEFT')
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data  = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }
    
    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败:' . $e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        return $this->fetch();
    }
    
    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('row', $row);
        return $this->fetch();
    }
    
    /**
     * @NodeAnotation(title="删除")
     */
    public function delete($id)
    {
        $this->checkPostRequest();
        $row = $this->model->whereIn('id', $id)->select();
        $row->isEmpty() && $this->error('数据不存在');
        try {
            $save = $row->delete();
        } catch (\Exception $e) {
            $this->error('删除失败');
        }
        $save ? $this->success('删除成功') : $this->error('删除失败');
    }
    /**
     * @NodeAnotation(title="启用/禁用")
     */
    public function modify()
    {
        $this->checkPostRequest();
        $post = $this->request->post();
        $rule = [
            'id|ID'    => 'require',
            'field|字段' => 'require',
            'value|值'  => 'require',
        ];
        $this->validate($post, $rule);
        $row = $this->model->find($post[ 'id' ]);
        if ( !$row) {
            $this->error('数据不存在');
        }
        if ( !in_array($post[ 'field' ], $this->allowModifyFields)) {
            $this->error('该字段不允许修改：' . $post[ 'field' ]);
        }
        try {
            $row->save([
                $post[ 'field' ] => $post[ 'value' ],
            ]);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
    }
//    /**
//     * @NodeAnotation(title="导出")
//     */
//    public function export()
//    {
//        [$page, $limit, $where] = $this->buildTableParames();
//        $header = [
//            ['id', 'id'],
//            ['标题', 'title'],
//            ['banner图片', 'image'],
//            ['排序', 'sort'],
//            ['状态', 'status'],
//            ['链接产品', 'goods_name'],
//            ['备注说明', 'remark'],
//            ['创建时间', 'create_time'],
//        ];
//        $list   = $this->model
//            ->withJoin('goods', 'LEFT')
//            ->where($where)
//            ->limit(100000)
//            ->order('id', 'desc')
//            ->select()
//            ->toArray();
//        foreach ($list as $key => $value) {
//            $list[ $key ][ 'status' ]     = $value[ 'status' ] == 1 ? '开启' : '关闭';
//            $list[ $key ][ 'goods_name' ] = $value[ 'goods' ][ 'title' ] ?? '';
//        }
//        $fileName = '轮播图列表' . time();
//        return Excel::exportData($list, $header, $fileName, 'xlsx');
//    }
}